/*
 * user_login_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../head/gui_callback/user_login_cb.h"
#include "../head/common_struct/userinfo.h"
#include "../head/communicate_with_server/userinfo_transfer.h"
#include "../head/gui_callback/global_cb.h"

extern GtkBuilder *builder;

//处理登录按钮的点击事件
void button_login_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkEntry *entry_login_username;
	GtkEntry *entry_login_password;
	GtkLabel *label_login_warn;

	const gchar * username;
	int username_length;
	const gchar * password;
	int password_length;

	entry_login_username =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_login_username"));
	entry_login_password =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_login_password"));
	label_login_warn =
			GTK_LABEL(gtk_builder_get_object(builder, "label_login_warn"));

	username = gtk_entry_get_text(entry_login_username);
	username_length = g_utf8_strlen(username, USERNAME_LEN);
	password = gtk_entry_get_text(entry_login_password);
	password_length = g_utf8_strlen(password, PASSWORD_LEN);

	if (username_length == 0 || password_length == 0) { //检查用户名和密码是否为空
		GtkWindow *global_window;
		global_window =
				GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));

		gtk_show_warning(global_window, "%s", "用户名或密码不能为空", "输入提醒");
		return;
	}

	struct login_request request;

	memset(&request, 0, sizeof(request));
	g_utf8_strncpy(request.username, username, username_length);
	g_utf8_strncpy(request.password, password, password_length);

	bool islogin = login(&request);
	if (islogin) { //登录成功
		set_login_flag(islogin);
		GtkToolbar * toolbar =
				GTK_TOOLBAR(gtk_builder_get_object(builder,"toolbar"));

		struct userinfo *user = get_user_info();
		char *user_type = user->type;
		if (is_manager(user_type)) { //用户是管理员

		} else { //用户不是管理员
			gtk_widget_show(GTK_WIDGET(toolbar) ); //显示工具栏
			window_show(GLOBAL_WELCOME_BODY); //显示欢迎界面
		}
		gtk_entry_set_text(entry_login_username, ""); //清空用户名输入框
		gtk_entry_set_text(entry_login_password, ""); //清空密码输入框
		gtk_label_set_label(label_login_warn, ""); //清空提示label
	} else {
		gtk_entry_set_text(entry_login_password, "");
		gtk_label_set_label(label_login_warn, "用户名或密码错误");
	}
}

//判断用户是否为管理员
bool is_manager(const char* type) {
	gint result = g_utf8_collate(type, MANAGER);
	if (result == 0) {
		return true;
	} else {
		return false;
	}
}

//处理新用户注册按钮的点击事件
void button_register_nologin_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkEntry *entry_register_username;
	GtkEntry *entry_register_password;
	GtkEntry *entry_reigster_password_again;
	GtkEntry *entry_register_detail_name;
	GtkRadioButton *radiobutton_register_detail_gender_male;
	GtkRadioButton *radiobutton_register_detail_gender_female;
	GtkEntry *entry_register_birthday;
	GtkCalendar *calendar_register_birthday;
	GtkComboBox *combobox_register_state;
	GtkComboBox *combobox_register_certificate_type;
	GtkEntry *entry_register_detail_certificate_id;
	GtkEntry *entry_register_contact_mobile_phone;
	GtkEntry *entry_register_contact_fixed_telephone;
	GtkEntry *entry_register_contact_email;
	GtkEntry *entry_register_contact_address;
	GtkEntry *entry_register_contact_postcode;
	GtkButton *button_login_noregister;

	entry_register_username =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_username"));
	entry_register_password =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_password"));
	entry_reigster_password_again =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_reigster_password_again"));
	entry_register_detail_name =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_detail_name"));
	radiobutton_register_detail_gender_male =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_register_detail_gender_male"));
	radiobutton_register_detail_gender_female =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_register_detail_gender_female"));
	entry_register_birthday =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_birthday"));
	calendar_register_birthday =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_register_birthday"));
	combobox_register_state = GTK_COMBO_BOX(
			gtk_builder_get_object(builder, "combobox_register_state"));
	combobox_register_certificate_type = GTK_COMBO_BOX(
			gtk_builder_get_object(builder,
					"combobox_register_certificate_type"));
	entry_register_detail_certificate_id =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_detail_certificate_id"));
	entry_register_contact_mobile_phone =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_mobile_phone"));
	entry_register_contact_fixed_telephone =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_fixed_telephone"));
	entry_register_contact_email =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_email"));
	entry_register_contact_address =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_address"));
	entry_register_contact_postcode =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_postcode"));
	button_login_noregister =
			GTK_BUTTON(gtk_builder_get_object(builder,"button_login_noregister"));

	gtk_entry_set_text(entry_register_username, "");
	gtk_entry_set_text(entry_register_password, "");
	gtk_entry_set_text(entry_reigster_password_again, "");
	gtk_entry_set_text(entry_register_detail_name, "");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_register_detail_gender_male), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_register_detail_gender_female), false);

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char birthday[BIRTHDAY_LEN];
	memset(birthday, 0, BIRTHDAY_LEN);
	sprintf(birthday, "%04d-%02d-%02d", tm_ptr->tm_year + 1900,
			tm_ptr->tm_mon + 1, tm_ptr->tm_mday);
	gtk_entry_set_text(entry_register_birthday, birthday);
	gtk_calendar_select_month(calendar_register_birthday, tm_ptr->tm_mon + 1,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_register_birthday, tm_ptr->tm_mday);

	gtk_combo_box_set_active(combobox_register_state, 0);
	gtk_combo_box_set_active(combobox_register_certificate_type, 0);
	gtk_entry_set_text(entry_register_detail_certificate_id, "");
	gtk_entry_set_text(entry_register_contact_mobile_phone, "");
	gtk_entry_set_text(entry_register_contact_fixed_telephone, "");
	gtk_entry_set_text(entry_register_contact_email, "");
	gtk_entry_set_text(entry_register_contact_address, "");
	gtk_entry_set_text(entry_register_contact_postcode, "");

	gtk_widget_show(GTK_WIDGET(button_login_noregister));
	window_show(REGISTER);
}
